package am.aua.renju.web.filldata;

import am.aua.renju.databaseaccess.Operations;
import am.aua.renju.entity.SecurityQuestion;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HeaderContain {

    private static void loadSecurityQuestion(Map root) {
        List<SecurityQuestion> securityQuestions = Operations.getInstance().getSecurityQuestins();
        int numOfSecurityQuestion = securityQuestions.size();
        String[] securityQuestionsText = new String[numOfSecurityQuestion];
        for (int i = 0; i < numOfSecurityQuestion; i++) {
            securityQuestionsText[i] = securityQuestions.get(i).getQuestionString();
        }
        root.put("securityQuestions", securityQuestionsText);
    }

    public static Map load() {
        Map root = new HashMap();
        loadSecurityQuestion(root);        
        root.put("loginError", "");
        root.put("nickname", "");
        root.put("loggedin", false);
        return root;
    }

    public static Map loadLoggedIn(String nickname) {
        Map root = new HashMap();
        loadSecurityQuestion(root);        
        root.put("loginError", "");
        root.put("nickname", nickname);
        root.put("loggedin", true);
        return root;
    }

    public static String loadRegistrationSuccessfull(String nickname) {
        return "{\"nickname\":\"" + nickname + "\"}";
    }

    public static class AjaxError {

        public String errorType;
        public String message;

        public AjaxError(String errorType, String message) {
            this.errorType = errorType;
            this.message = message;
        }
    }

    public static String loadErrorRegistrationEmail() {
        Gson gson = new Gson();
        AjaxError data = new AjaxError("email", "There is already a registred user with this email. Please change your email.");
        return gson.toJson(data, AjaxError.class);
    }

    public static String loadErrorRegistrationNickName() {
        Gson gson = new Gson();
        AjaxError data = new AjaxError("nickname", "There is already a registred user with this nickname. Please change your nickname.");
        return gson.toJson(data, AjaxError.class);
    }

    public static Map loadLoginError() {
        Map root = new HashMap();
        loadSecurityQuestion(root);
        root.put("nickname", "");
        root.put("loggedin", false);
        root.put("loginError", "The email and/or password is wrong.");
        root.put("registrationError", "");
        return root;
    }
}
